/*
 * Project Name: jmax
 * File Name: ReceiveServer.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.hotel.protocol.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.core.config.PropertiesConfigurer;
import com.xdtech.socket.server.SocketUtil;

/**
 * 通信接收服务，采用socket监听
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-4-2 下午2:57:18
 */
public class ReceiveServer
{
	private static Log log = LogFactory.getLog(ReceiveServer.class);

	/**
	 * 启动web 监听接收设备上传数据信息服务
	 * 
	 * @author max.zheng
	 * @since 2015-4-2 下午3:49:21
	 */
	public static void runningReceiveService()
	{
		startSocketServer();

	}

	/**
	 * 启动socket服务
	 * 
	 * @author max.zheng
	 * @since 2015-4-2 下午4:09:28
	 */
	private static void startSocketServer()
	{
		try
		{
			String port = PropertiesConfigurer.getValue("hotel.monitor.port");
			final ServerSocket ss = new ServerSocket(Integer.valueOf(port));
			log.info("Monitoring Socket Server ........");
			new Thread(new Runnable()
			{
				@Override
				public void run()
				{
					while (true)
					{
						Socket socket = null;
						InputStream inputStream = null;
						OutputStream outputStream = null;
						boolean receiveSuccess = false;
						try
						{
							socket = ss.accept();
							//限制客户端5秒内不发送请求自动关闭
							socket.setSoTimeout(5000);
							InetAddress inetAddress = socket.getInetAddress();
//							log.info("通信客户端ip="+socket.getInetAddress());
							inputStream = socket.getInputStream();
							outputStream = socket.getOutputStream();
							
							String receiveData = SocketUtil.readStrFromStream(inputStream);
							log.info("\nWeb Server receive data=" + receiveData);
							
							ReceiveHandler.handlerEvent(inetAddress.getHostAddress(),receiveData);

							outputStream.write(("\nWeb Server receive data=" + receiveData).getBytes());
							receiveSuccess = true;
						}
						catch (Exception e)
						{
							receiveSuccess = false;
							log.error("Monitoring Socket Server Error On Thread", e);
						}
						finally
						{
							try
							{
								if (!receiveSuccess)
								{
									log.info("-----------connect timeout not send data");
									outputStream.write("\nconnect timeout not send data".getBytes());
								}
								if (null != inputStream)
								{
									inputStream.close();
								}
								if (null != outputStream)
								{
									outputStream.close();
								}
								if (null != socket)
								{
									socket.close();
								}
							}
							catch (IOException e)
							{
								log.info("Monitoring Socket Server Close Error",e);
							}
						}
					}
				}
			}, "SocketRunningThread").start();
			log.info("Monitoring Socket Server ........OK");
		}
		catch (IOException e)
		{
			log.error("Monitoring Socket Server Error", e);
		}

	}

}
